package com.zbkj.front.service.monitor;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.zbkj.common.model.monitor.ZfbMonitorEquipment;
import com.zbkj.common.monitorchannel.vo.MonitorChannelMediaAddressVo;
import com.zbkj.common.response.ProductDetailResponse;
import com.zbkj.service.monitor.MonitorChannelHandle;
import com.zbkj.service.service.monitor.IZfbMonitorEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: ProductMonitorService
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/6/29 18:40
 * @Version: V1.0
 **/
@Service
@Slf4j
public class ProductMonitorService {

    @Autowired
    private MonitorChannelHandle monitorChannelHandle;

    @Autowired
    private IZfbMonitorEquipmentService zfbMonitorEquipmentService;


    public ProductDetailResponse fillProductDetailResponseMediaAddress(ProductDetailResponse response) {
        long startTime = System.currentTimeMillis();
        if (ObjectUtil.isNull(response.getProductInfo())) {
            return response;
        }
        if (StrUtil.isBlank(response.getProductInfo().getLicense())) {
            return response;
        }
        List<String> licenseList = StrUtil.split(response.getProductInfo().getLicense(), ',');
        licenseList.removeIf(o-> StrUtil.isBlank(o));
        response.setLicense(licenseList);

        List<MonitorChannelMediaAddressVo> mediaAddressList = Lists.newArrayList();
        licenseList.forEach(license -> {
            ZfbMonitorEquipment monitorEquipment = zfbMonitorEquipmentService.getOne(new LambdaQueryWrapper<ZfbMonitorEquipment>().eq(ZfbMonitorEquipment::getLicense,license));
            if (ObjectUtil.isNotNull(monitorEquipment)) {
                MonitorChannelMediaAddressVo mediaAddress = monitorChannelHandle.getMediaAddress(monitorEquipment.getLicense(), monitorEquipment.getEquipmentType());
                mediaAddressList.add(mediaAddress);
                log.info("#fillProductDetailResponseMediaAddress#监控流媒体地址耗时：{}", System.currentTimeMillis() - startTime);
            }
        });
        response.setMonitorMediaAddressList(mediaAddressList);
        return response;
    }
}
